package com.pdf.pdfserver.vo;

import org.apache.commons.lang3.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Author songfz
 * @Description solr 基本配置数据
 * @Date 2022/1/12
 */
public class SolrHostVO {

    /**
     * 账号
     */
    private String userName;
    /**
     * 密码
     */
    private String password;
    /**
     * host地址
     */
    private String host;
    /**
     * 端口号
     */
    private Integer port;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public SolrHostVO() {
    }

    public SolrHostVO(String userName, String password, String host, Integer port) {
        this.userName = userName;
        this.password = password;
        this.host = host;
        this.port = port;
    }

    /**
     * 解析地址
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public SolrHostVO parse(String url) throws MalformedURLException {

        URL u = new URL(url);
        String userName = "";
        String password = "";
        String host = u.getHost();
        int port = u.getPort();

        String userInfo = u.getUserInfo();
        String[] users = StringUtils.isNotBlank(userInfo)?userInfo.split(":"):new String[0];

        if(users.length>0){

             userName = users[0];
             password = users[1];
        }
        return new SolrHostVO(userName,password,host,port);
    }

    @Override
    public String toString() {
        return "SolrHostVO{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", host='" + host + '\'' +
                ", port=" + port +
                '}';
    }
}
